package com.xmu.edu.tisp.service.contract;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xmu.edu.tisp.dao.Service;
import com.xmu.edu.tisp.dao.TISP_dbHelper;

public class TP_FIELD_BLOCK_service implements Service{
	

	public static final String TABLE_NAME = "TP_FIELD_BLOCK";
	public static final String PRIMARY_KEY="FIELD_BLOCK_ID";
	
	private SQLiteDatabase myDB;
	private SQLiteOpenHelper dbHelper;


	public TP_FIELD_BLOCK_service(Context cxt) {
		dbHelper=new TISP_dbHelper(cxt, "TISP");
	}

	public SQLiteDatabase getdb()
	{
		if(myDB==null)
		{
			myDB=dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}
	
	@Override
	public void g_updata(ContentValues values) {
		getdb().update(TABLE_NAME, values, PRIMARY_KEY+"=?", new String[]{(String) values.get(PRIMARY_KEY)});
		System.out.println(TABLE_NAME+"have updataed!");
		
	}

	@Override
	public void g_insert(ContentValues values) {
		getdb().insert(TABLE_NAME, null, values);
		
		System.out.println(TABLE_NAME+"have inserted!");
	}

	@Override
	public void g_delete(String[] id) {
		getdb().delete(TABLE_NAME, PRIMARY_KEY+"=?", id);
		System.out.println(TABLE_NAME+"have deleted!");
		
	}


	@Override
	public List<String> g_getIDs() {
		Cursor c=getdb().query(TABLE_NAME, new String[]{PRIMARY_KEY},null, null, null, null, null);
		List<String> result=new ArrayList<String>();
		while(c.moveToNext())
		{
			String id=c.getString(c.getColumnIndex(PRIMARY_KEY));
			result.add(id);
		}
		return result;
	}
	
	public Cursor fetchDataBy_Farmer(String Farmer_Name){
		
		Cursor cursor=getdb().rawQuery("select FIELD_BLOCK_ID,FIELD_BLOCK_CODE,FIELD_MANAGER  " + 
				"from TP_FIELD_BLOCK " + 
				"where TP_FIELD_BLOCK.FIELD_MANAGER=? and TP_FIELD_BLOCK.FIELD_BLOCK_ID NOT IN (select FIELD_BLOCK_ID from XJSZ_ZYMD_DETAIL);"
				, new String [] {Farmer_Name});
		
        cursor.deactivate();
        return cursor;
	}
	
	public Cursor fetchDataExcept_Farmer(String Farmer_Name){
		
		Cursor cursor=getdb().rawQuery("select FIELD_BLOCK_ID,FIELD_BLOCK_CODE,FIELD_MANAGER  " + 
				"from TP_FIELD_BLOCK " + 
				"where FIELD_MANAGER<>? AND TP_FIELD_BLOCK.FIELD_BLOCK_ID NOT IN (select FIELD_BLOCK_ID from  XJSZ_ZYMD_DETAIL);"
				, new String [] {Farmer_Name});
		
        cursor.deactivate();
        return cursor;
	}
	
    public Cursor fetchXJSZ_ZYSQQR_DataBy_Farmer(String Farmer_Name){
		
		Cursor cursor=getdb().rawQuery("select FIELD_BLOCK_ID,FIELD_BLOCK_CODE,FIELD_MANAGER  " + 
				"from TP_FIELD_BLOCK " + 
				"where TP_FIELD_BLOCK.FIELD_MANAGER=? and TP_FIELD_BLOCK.FIELD_BLOCK_ID NOT IN (select FIELD_BLOCK_ID from XJSZ_ZYSQQR_DETAIL);"
				, new String [] {Farmer_Name});
		
        cursor.deactivate();
        return cursor;
	}
    
   public Cursor fetchXJSZ_ZYSQQR_DataExcept_Farmer(String Farmer_Name){
		
		Cursor cursor=getdb().rawQuery("select FIELD_BLOCK_ID,FIELD_BLOCK_CODE,FIELD_MANAGER  " + 
				"from TP_FIELD_BLOCK " + 
				"where TP_FIELD_BLOCK.FIELD_MANAGER<>? and TP_FIELD_BLOCK.FIELD_BLOCK_ID NOT IN (select FIELD_BLOCK_ID from XJSZ_ZYSQQR_DETAIL);"
				, new String [] {Farmer_Name});
		
        cursor.deactivate();
        return cursor;
	}
	
	
	public Cursor fetchDataByCD(String FIELD_BLOCK_CODE){
		
		System.out.println(FIELD_BLOCK_CODE);
		Cursor cursor=getdb().rawQuery("select FIELD_BLOCK_ID,FIELD_BLOCK_CODE,FIELD_MANAGER,FIELD_BLOCK_ATEA  " + 
				"from TP_FIELD_BLOCK " + 
				"where FIELD_BLOCK_CODE=?;"
				, new String [] {FIELD_BLOCK_CODE});
		
        cursor.deactivate();
        return cursor;
	}
	
	public Cursor fetchDataByID(String FIELD_BLOCK_ID){
		
		System.out.println(FIELD_BLOCK_ID);
		Cursor cursor=getdb().rawQuery("select *  " + 
				"from TP_FIELD_BLOCK " + 
				"where FIELD_BLOCK_ID=?;"
				, new String [] {FIELD_BLOCK_ID});
		
        cursor.deactivate();
        return cursor;
	}
	

}

